<script setup>
import {getExamineAll, updateStatus, addExamine, getExamineById, deleteExamine} from "@/api/system/park/examine"
import {listTree} from "@/api/system/park/building"

export default {
  data() {
    return {
      dialogVisible: false,
      total: 1,
      bulidingTree: [],
      floorList: [],
      roomList: [],
      queryParams: {
        pageNum: 1,
        pageSize: 3
      },
      form: {},
      tableData: [],
      ruleForm: {
        id: "",
        bulidingId: "",
        floorId: "",
        roomId: ""

      },
      rules: {
        applyforName: [
          {required: true, message: '请输入申请人姓名', trigger: 'blur'},
        ],
        idcardNumber: [
          {required: true, message: '请输入申请人身份证号码', trigger: 'blur'},
        ]
      },
    }
  },
  methods: {
    queryMethods() {
      let that = this;
      getExamineAll(that.queryParams).then(function (data) {
        that.tableData = data.data.list
        that.queryParams.pageNum = data.data.pageNum;
        that.queryParams.pagSize = data.data.pageSize;
        that.total = data.data.total;
      })
    },
    addExamineShow() {
      this.ruleForm = {
        id: "",
        bulidingId: "",
        floorId: "",
        roomId: ""
      }
      this.dialogVisible = true;
    },
    resetQuery() {
      this.queryParams = {};
      this.resetForm("queryParams");
      this.queryMethods();
    },
    handleCurrentChange(pageNum) {
      this.queryParams.pageNum = pageNum;
      this.queryMethods();
    },
    handleSizeChange(size) {
      this.queryParams.pageSize = size;
      this.queryMethods();
    },
    shenhe(data, status) {
      console.log(data)
      let that = this;
      that.form.id = data.id;
      that.form.applyforStatus = status;
      that.form.journalNumber = data.journalNumber;
      updateStatus(that.form).then(function (data) {
        if (data.code == 200) {
          that.$message.success(data.message)
        } else {
          that.$message.success(data.message)
        }
        that.queryMethods();
      })
    },
    changBuliding() {
      let that = this;

      that.floorList = null;
      that.ruleForm.floorId = "";
      that.roomList = null;
      that.ruleForm.roomId = "";

      let chooseBulidingId = that.ruleForm.bulidingId;

      for (let i = 0; i < that.bulidingTree.length; i++) {
        if (that.bulidingTree[i].id == chooseBulidingId) {
          that.floorList = that.bulidingTree[i].children;
        }
      }
    },
    changFloor() {
      let that = this;

      that.roomList = null;
      that.ruleForm.roomId = "";

      let chooseFloorId = that.ruleForm.floorId;

      for (let i = 0; i < that.floorList.length; i++) {
        if (that.floorList[i].id == chooseFloorId) {
          that.roomList = that.floorList[i].children;
        }
      }

    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    submitForm(formName) {
      let that = this;
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (that.ruleForm.id == "" || that.ruleForm.id == undefined) {
            addExamine(that.ruleForm).then((res) => {
              if (res.code == 200) {
                that.$message.success("添加成功");
              } else {
                that.$message.error("添加失败")
              }
              that.dialogVisible = false;
              that.queryMethods();
            })
          } else {
            console.log("修改")
          }
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    updateShow(id) {
      getExamineById(id).then((res) => {
        this.ruleForm = res.data;
        this.changBuliding()
        this.changFloor()
      })

      this.dialogVisible = true;

    },
    delExamine(id) {
      let that = this;
      deleteExamine(id).then((res) => {
        if (res.code == 200) {
          that.$message.success("删除成功");
        } else {
          that.$message.error("删除失败")
        }
        that.queryMethods();
      })
    }

  },
  created() {
    let that = this;
    that.queryMethods();
    listTree().then(function (data) {
      that.bulidingTree = data.data;
    })
  }
}
</script>

<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true">
      <el-form-item label="公寓名称">
        <el-input
          v-model="queryParams.applyforLocation"
          placeholder="请输入公寓名称"
          clearable
          size="small"
          style="width: 200px"
        />
      </el-form-item>
      <el-form-item label="用户名称">
        <el-input
          v-model="queryParams.applyforName"
          placeholder="请输入用户名称"
          clearable
          size="small"
          style="width: 200px"
        />
      </el-form-item>
      <el-form-item label="企业名称">
        <el-select
          v-model="queryParams.applyforUnit"
          placeholder="企业名称"
          clearable
          size="small"
          style="width: 200px"
        >

        </el-select>
      </el-form-item>
      <el-form-item label="状态">
        <el-select
          v-model="queryParams.applyforStatus"
          placeholder="状态"
          clearable
          size="small"
          style="width: 240px"
        >
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="queryMethods()">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-button type="primary" style="margin-top:30px;margin-bottom: 30px" icon="el-icon-plus" @click="addExamineShow">
      添加
    </el-button>
    <el-table
      :data="tableData"
      stripe
      style="width: 100%">
      <el-table-column
        prop="id"
        label="序号"
        align="center"
        width="80">
      </el-table-column>
      <el-table-column
        prop="journalNumber"
        label="流水号"
        align="center"
        width="100">
      </el-table-column>
      <el-table-column
        prop="applyforName"
        label="申请人姓名"
        align="center"
        width="100">
      </el-table-column>
      <el-table-column
        prop="idcardNumber"
        label="身份证号码"
        align="center"
        width="180">
      </el-table-column>
      <el-table-column
        prop="applyforPhone"
        label="申请人手机号码"
        align="center"
        width="130">
      </el-table-column>
      <el-table-column
        prop="applyforUnit"
        label="申请人单位"
        align="center"
        width="100">
      </el-table-column>
      <el-table-column
        prop="applyforCause"
        label="申请人原因"
        align="center"
        width="100">
      </el-table-column>
      <el-table-column
        prop="applyforLocation"
        label="申请位置"
        align="center"
        width="100">
      </el-table-column>
      <el-table-column
        prop="applyforCheckinTime"
        label="申请入住时间"
        align="center"
        width="110">
      </el-table-column>
      <el-table-column
        prop="applyforDwellTime"
        label="申请居住时间段"
        align="center"
        width="120">
      </el-table-column>
      <el-table-column
        label="审批状态"
        align="center"
        width="100">
        <template slot-scope="scope">
          <span v-if="scope.row.applyforStatus==1">
            <el-tag type="warning">未审核</el-tag>
          </span>
          <span v-if="scope.row.applyforStatus==2">
            <el-tag type="success">通过</el-tag>
          </span>
          <span v-if="scope.row.applyforStatus==3">
            <el-tag type="danger">拒绝</el-tag>
          </span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        width="230">
        <template slot-scope="scope">
          <el-tooltip content="编辑">
            <el-button type="primary" icon="el-icon-edit" @click="updateShow(scope.row.id)" circle></el-button>
          </el-tooltip>
          <span v-if="scope.row.applyforStatus==1">
          <el-tooltip content="通过">
            <el-button type="success" icon="el-icon-check" @click="shenhe(scope.row,2)" circle></el-button>
          </el-tooltip>
          <el-tooltip content="拒绝">
            <el-button type="warning" icon="el-icon-star-off" @click="shenhe(scope.row,3)" circle></el-button>
          </el-tooltip>
          </span>
          <el-tooltip content="删除">
            <el-button type="danger" icon="el-icon-delete" @click="delExamine(scope.row.id,3)" circle></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>
    <div class="block">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="this.queryParams.pageNum"
        :page-size="this.queryParams.pageSize"
        :page-sizes="[1,2,3]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </div>
    <el-dialog
      title="添加公寓审批"
      :visible.sync="dialogVisible"
      width="30%">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="120px" class="demo-ruleForm">
        <el-form-item label="申请人姓名" prop="applyforName">
          <el-input v-model="ruleForm.applyforName"></el-input>
        </el-form-item>
        <el-form-item label="身份证号码" prop="applyforName">
          <el-input v-model="ruleForm.idcardNumber"></el-input>
        </el-form-item>
        <el-form-item label="申请人手机号">
          <el-input v-model="ruleForm.applyforPhone"></el-input>
        </el-form-item>
        <el-form-item label="申请人单位">
          <el-input v-model="ruleForm.applyforUnit"></el-input>
        </el-form-item>
        <el-form-item label="申请人原因">
          <el-input v-model="ruleForm.applyforCause"></el-input>
        </el-form-item>
        <el-form-item label="申请位置">
          <el-select
            @change="changBuliding"
            v-model="ruleForm.bulidingId"
            placeholder="楼栋类型"
            clearable
            size="small"
            style="width: 240px"
          >
            <el-option v-for="o in bulidingTree"
                       :key="o.buildingName"
                       :label="o.buildingName"
                       :value="o.id"

            ></el-option>
          </el-select>
          <el-select
            @change="changFloor"
            v-model="ruleForm.floorId"
            placeholder="楼层"
            clearable
            size="small"
            style="width: 240px"
          >
            <el-option v-for="o in floorList"
                       :key="o.floorName"
                       :label="o.floorName"
                       :value="o.id"

            ></el-option>
          </el-select>
          <el-select
            v-model="ruleForm.roomId"
            placeholder="房间"
            clearable
            size="small"
            style="width: 240px"
          >
            <el-option v-for="o in roomList"
                       :key="o.roomName"
                       :label="o.roomName"
                       :value="o.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="申请入住时间">
          <el-date-picker
            v-model="ruleForm.applyforCheckinTime"
            type="date"
            placeholder="选择日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="申请入住时间段">
          <el-input v-model="ruleForm.applyforDwellTime"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<style scoped lang="scss">

</style>
